import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

typedef _CallBack = void Function(String roomCode);

typedef _CloseFarme = void Function();

class CommonAlterDialog extends StatefulWidget {
  final _CallBack callback;
  final _CloseFarme closeFarme;
  final EdgeInsets insertPanding;
  final Widget child;
  final double height;
  final double width;
  final Color backgroundColor;
  static void defaultCloseFarme() {}
  CommonAlterDialog(
      {this.callback,
      this.insertPanding,
      this.child,
      this.height,
      this.width,
      this.backgroundColor,
      this.closeFarme = defaultCloseFarme});
  @override
  _CommonAlterDialogState createState() => _CommonAlterDialogState();
}

class _CommonAlterDialogState extends State<CommonAlterDialog> {
  EdgeInsets _defaultInsetPadding =
      EdgeInsets.symmetric(horizontal: 30.w, vertical: 10.h);
  @override
  void initState() {
    super.initState();
    if (widget.insertPanding != null) {
      _defaultInsetPadding = widget.insertPanding;
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: SizedBox(
            //再用SizeBox指定宽度
            width:
                widget.width == null ? ScreenUtil().screenWidth : widget.width,
            child: Dialog(
              elevation: 0.0,
              insetPadding: _defaultInsetPadding,
              backgroundColor: Colors.transparent,
              child: Container(
                // 因为内部 margin 了 30.h 所以这里要加上去才是外部的真实高度
                height: widget.height == null
                    ? ScreenUtil().screenHeight
                    : widget.height + 30.h,
                width: widget.width == null
                    ? ScreenUtil().screenWidth + 30.h
                    : widget.width,
                child: Stack(
                  children: [
                    Container(
                      height: double.infinity,
                      width: double.infinity,
                      margin: EdgeInsets.all(10.h),
                      decoration: BoxDecoration(
                        boxShadow: [
                          BoxShadow(
                              color: Colors.white.withOpacity(0.2),
                              spreadRadius: 1.0,
                              blurRadius: 1.0),
                        ],
                        borderRadius: BorderRadius.circular(8.0),
                      ),
                    ),
                    Container(
                      height: double.infinity,
                      width: double.infinity,
                      decoration: BoxDecoration(
                        color: widget.backgroundColor,
                      ),
                      margin: EdgeInsets.all(15.h),
                      child: widget.child == null ? Container() : widget.child,
                    ),
                    Align(
                        alignment: Alignment(1, -1),
                        child: InkWell(
                          onTap: () {
                            widget.closeFarme();
                            Navigator.of(context).pop();
                          },
                          borderRadius: BorderRadius.circular(40.h),
                          child: ClipOval(
                            child: Container(
                              height: 30.h,
                              width: 30.h,
                              decoration: BoxDecoration(
                                  color: Colors.blue[300],
                                  borderRadius: BorderRadius.circular(40.h),
                                  border: Border.all(
                                      color: Colors.white.withOpacity(0.3),
                                      width: 3.h)),
                              child: Icon(
                                Icons.close,
                                color: Colors.orange[200],
                                size: 20.h,
                              ),
                            ),
                          ),
                        )),
                  ],
                ),
              ),
            )));
  }
}
